<template>
	<view style="padding: 0 10rpx;">
		<uni-section title="基本设置" class="topfixed-active" type="line">
			<button type="default" size="mini" v-if="!isEdit" style="margin: 0 auto;background-color: #007aff; color: #fff;" @click="edit">修改</button>
			<button type="default" size="mini" v-else style="margin: 0 auto;background-color: red; color: #fff;" @click="cacleEdit">取消</button>
		</uni-section>
		<view  style="margin-top: 60px;">
			<text class="tips"> 注:此处的设置对所有团队生效,如果团队需要单独设置,请到团队的设置页面进行修改!</text>
			<uni-forms 
			ref="baseForm" 
			labelAlign="left" 
			:modelValue="configs" 
			:rules="rules">
				<uni-forms-item label="加班系数:" name="extRate">
					<uni-easyinput :disabled="!isEdit" v-model="configs.extRate" placeholder="加班工资系数" />
				</uni-forms-item>
				<uni-forms-item label="出差系数:" name="errandRate">
					<uni-easyinput :disabled="!isEdit" v-model="configs.errandRate" placeholder="出差工资系数" />
				</uni-forms-item>
				<uni-forms-item label="出差补助:" name="errandExtendSalary">
					<uni-easyinput :disabled="!isEdit" v-model="configs.errandExtendSalary" placeholder="出差工资补助/天" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" v-if="isEdit == true" @click="saveConfig" style="background-color: #007aff;">提交</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {},
				isEdit: false,
				teamId: null,
				configs:{
					extRate: 1.00,
					errandRate: 1.00,
					errandExtendSalary: 0,
				},
				rules: {
					extRate: {
						rules: [{
							format: 'number',
							errorMessage: '必须输入数字'
						}]
					},
					errandRate: {
						rules: [{
							format: 'number',
							errorMessage: '必须输入数字'
						}]
					},
					errandExtendSalary: {
						rules: [{
							format: 'number',
							errorMessage: '必须输入数字'
						}]
					}
				},
			}
		},
		onLoad(ev) {
			if(ev.teamId) {
				this.teamId = ev.teamId;
			}
			this.user = this.$dry.get("user");
			this.getConfig(ev.teamId);
			
		},
		onReady() {
			// 需要在onReady中设置规则
			this.$refs.baseForm.setRules(this.rules)
		},
		methods: {
			edit(){
				this.isEdit = true;
			},
			cacleEdit() {
				this.isEdit = false;
			},
			getConfig(teamId) {
				var token = this.$dry.getToken();
				if(token) {
					var data = this.$request.run("getConfig", token);
					var queryData = {
						userId: this.user.userId
					};
					if(teamId) {
						queryData.teamId = teamId;
					}
					uni.request({
						url: data.url,
						method: data.method,
						header: data.header,
						data: queryData,
						success: data => {
							if(data.statusCode === 200) {
								if(data.data != '') {
									this.configs = data.data;
								}
							}
						}
					})
				}
			},
			saveConfig() {
				this.$refs['baseForm'].validate().then(res => {
					var token = this.$dry.getToken();
					if(token) {
						var data = this.$request.run("addOrUpdateConfig", token);
						if(this.teamId != null) {
							this.configs.teamId = this.teamId;
						}
						uni.request({
							url: data.url,
							method: data.method,
							header: data.header,
							data: this.configs,
							success: data => {
								if(data.statusCode === 200) {
									uni.showToast({
										title: "修改成功"
									})
									this.getConfig(this.teamId);
								}
							}
						})
					}
					this.isEdit = false;
				});
			},
		}
	}
</script>

<style>
.tips{
	padding:10rpx 20rpx;
	text-align: left;
	font-size: 24rpx;
	color: #0077AA;
}
.topfixed-active{
	width: 100%;
	height: 50px;
	position: fixed;
	top: var(--window-top);
	left: 0;
	background: #fff;
	z-index: 9;
	box-sizing: border-box;
}
</style>
